package com.scalar.chiptrack.finance.creditNote.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.finance.creditNote.actionCommands.CreditNoteActionCommandFactory;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * User: Sujan
 * Date: Apr 13, 2007
 * Time: 2:30:38 PM
 */
public class CreditNoteAction extends ChipTrackAction
{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        ActionForward actionForward = mapping.findForward ( "success" );
        String action   = request.getParameter ( "action" );
        HashMap errors  = new HashMap();
        HashMap info    = new HashMap();

        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        HttpSession mSession    = request.getSession();
        User user               = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

         // Redirect the user to login page
        if( user == null )
        {
            mSession.setAttribute( SessionKeys.SOURCE_PAGE_KEY, "/creditNoteAction.do" );
            return( mapping.findForward( SessionKeys.ACTION_FORWARD_KEY_LOGIN ) );
        }
        try
        {
            if ( action == null )
            {
                action = Constants.ACTION_SEARCH;
            }

            ActionCommand actionCommand = CreditNoteActionCommandFactory.getActionCommand( action );

            if ( actionCommand != null )
            {
                actionForward = actionCommand.execute( mapping, form, request, response );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        return actionForward;
    }
}
